package  
{
	import flash.display.MovieClip;
	/**
	 * ...
	 * @author Rodrigo Forti
	 */
	public class Inimigo extends MovieClip
	{
		public var listaPedacosInimigo:Array;
		private var baixando:Boolean;
		private var luzRandomica:uint;
		public var direcaoRandomica:uint;
		public var subindo:Boolean;
		public var filaDownload:Array;
		private var posicaoRandomica:uint;
		private var download_atual:uint;
		
		private var luz:Object;
		private var k:uint;
		
		public function Inimigo() 
		{			
			listaPedacosInimigo = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
			filaDownload = new Array();
			download_atual = 24;
			baixando = false;
			direcaoRandomica = Math.floor(Math.random() * 4);
		
			if (direcaoRandomica == 0)
			{
				this.x = 600 + this.width / 2;
				this.y = this.height / 2 + Math.floor(Math.random() * (600 - this.height))
				while (Math.abs(this.y - Main.getInstance().getHeroiY()) < this.height) {//evita criar inimigo em rota de colisão com heroi
					//trace("Colision path y: "+this.y+" and "+Main.getInstance().getHeroiY())
					this.y = this.height / 2 + Math.floor(Math.random() * (600 - this.height))
				}
			}
			
			else if (direcaoRandomica == 1)
			{
				this.x = 0 - this.width / 2;
				this.y = this.height/2 + Math.floor(Math.random()* (600 - this.height))
				while (Math.abs(this.y - Main.getInstance().getHeroiY()) < this.height) {//evita criar inimigo em rota de colisão com heroi
					//trace("Colision path y: "+this.y+" and "+Main.getInstance().getHeroiY())
					this.y = this.height / 2 + Math.floor(Math.random() * (600 - this.height))
				}
			}
			
			else if (direcaoRandomica == 2)
			{
				this.x = this.width / 2 + Math.floor(Math.random() * (600 - this.width))
				while (Math.abs(this.x - Main.getInstance().getHeroiX()) < this.width) {//evita criar inimigo em rota de colisão com heroi
					//trace("Colision path x: "+this.x+" and "+Main.getInstance().getHeroiX())
					this.x = this.width / 2 + Math.floor(Math.random() * (600 - this.width))
				}
				this.y = Main.getStage().stageHeight + this.height/2
			}
			
			else if (direcaoRandomica == 3)
			{
				this.x = this.width / 2 + Math.floor(Math.random() * (600 - this.width))
				while (Math.abs(this.x - Main.getInstance().getHeroiX()) < this.width) {//evita criar inimigo em rota de colisão com heroi
					//trace("Colision path x: "+this.x+" and "+Main.getInstance().getHeroiX())
					this.x = this.width / 2 + Math.floor(Math.random() * (600 - this.width))
				}
				this.y = 0 - this.height/2
			}
			

			var n:uint;
			n = Math.min(Math.max(Math.random() * Main.tempo / 3, 1), 15)
			
			for (var i:uint = 0 ; i < n ; i++)
			{		
				luzRandomica = Math.floor(Math.random() * 16);
				listaPedacosInimigo[luzRandomica] = 1;
				luz = this.getChildByName("luz" + luzRandomica);
				luz.gotoAndStop(30);
			}
		}
		
		public function update():void
		{	
			if (baixando == false && subindo == false )
			{
				if (direcaoRandomica == 0)
				{
					this.x -= 5;
				}
				
				else if (direcaoRandomica == 1)
				{
					this.x += 5;
				}
				
				else if (direcaoRandomica == 2)
				{
					this.y -= 5;
				}
				
				else if (direcaoRandomica == 3)
				{
					this.y += 5;
				}
			}
			
			else
			{
				if (luz.currentFrame == 30)
				{
					if (filaDownload.length >= 1)
					{
						k = filaDownload.pop();
						listaPedacosInimigo[k] = 1;
						luz = this.getChildByName("luz" + k);
						luz.gotoAndPlay(2);
						download_atual = k;
					}
					
					else
					{
						Heroi.subindo = false;
						download_atual = 24
						this.baixando = false;
					}
				}
			}
			
			if (baixando) {
				contorno.gotoAndStop("green");
			}else if (subindo) {
				contorno.gotoAndStop("red");
			}else {
				contorno.gotoAndStop("none");
			}
			
		}
		
		public function download(k:uint):void
		{
			this.k = k
			
			if (download_atual == 24)
			{
				listaPedacosInimigo[k] = 1;
				luz = this.getChildByName("luz" + k);
				luz.gotoAndPlay(2);
				download_atual = k;
			}
			
			else
			{
				filaDownload.push(k);
			}
			
			if (baixando == false)
			{
				baixando = true;
			}
		}
		
		public function upload(k:uint):void
		{
			subindo = true;
		}

	}

}